
          /*
           -------ProductDao By Raj Vallamsetla------
  		   
				For the insertion of Base Product 

          */

package com.scalar.chiptrack.operations.process.dao;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.scalar.ScalarException;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.db.DataSource;
import com.scalar.chiptrack.operations.process.forms.FabProcessSplitForm;
import com.scalar.chiptrack.utils.DBLoader;
import com.scalar.chiptrack.utils.SqlQueryMappings;

public class FabProcessSplitDAO 
{

 private static DBLoader s_dbLoader = null;

 
     public FabProcessSplitDAO()
	  {
	
	  }

	// This method is to check whether the fab data all ready exists for the baseProduct

	public String getProductId(String ProductId) throws ScalarException	
    {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs    = null;
		String productId   = "";
			 
		try
		{
			conn = DataSource.getConnection();		
       		pstmt = conn.prepareStatement(SqlQueryMappings.get ("operations.process.product.checkByProductId"));
       		pstmt.setString(1,ProductId);
	   		rs = pstmt.executeQuery();
			
			while(rs.next())
			{
				productId = rs.getString(1);
		    }
		}
		catch (Exception sqle) 
		{
        	sqle.printStackTrace();
            throw new ScalarException ("Could not get Part Number from database", sqle);
		}
        finally 
        {
            if (rs != null) {
                try { rs.close(); } catch (SQLException ignore) {}
            }
            if (pstmt != null) {
                try { pstmt.close(); } catch (SQLException ignore) {}
            }
            if (conn != null) {
                try { conn.close(); } catch (SQLException ignore) {}
            }
		}
		return productId;
	} 

// Ends Here

	public FabProcessSplitForm getFabTrackId(String ProductId)	throws ScalarException
    {
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs     = null;
		String trackId   = "";
		String fabName	  = "";
		String check	  = "";
		ArrayList fabTrackList= new ArrayList();
		ArrayList fabProductList=new ArrayList(); 

		FabProcessSplitForm fabProcessSplitForm = new FabProcessSplitForm();
				 
		try{
				
		   conn = DataSource.getConnection();
		   pstmt = conn.prepareStatement(SqlQueryMappings.get ("operations.process.product.fabTrackId"));
		   pstmt.setString(1,ProductId);
		   rs = pstmt.executeQuery();
		
		

			while(rs.next())
			{
				  trackId = rs.getString(1);
				  fabName = rs.getString(2);
				  check="exists";
				  fabTrackList.add(trackId);
				  fabProductList.add(fabName);	
				 
			}
 
			if(check.equals(""))
				fabProcessSplitForm = null;
		
			if(fabProcessSplitForm!=null){
							
				int len =  fabTrackList.size();
				String [] fabTrackId = new String [len]	;
				String [] fabNameList = new String [len];

				for(int i=0;i<fabTrackList.size();i++){
				
					fabTrackId[i]=(String)fabTrackList.get(i);	
					fabNameList[i]=(String)fabProductList.get(i);


				}
				fabProcessSplitForm.setFabTrackId(fabTrackId);
				fabProcessSplitForm.setFabProduct(fabNameList);

							
			}
    
		   }catch(Exception e){
				e.printStackTrace();
				throw new ScalarException("get Fab Track failed ", e);
		   }
		   finally 
		   {
		   		if (rs != null) {
					try { rs.close(); } catch (SQLException ignore) {}
				}
				if (pstmt != null) {
					try { pstmt.close(); } catch (SQLException ignore) {}
				}
				if (conn != null) {
					try { conn.close(); } catch (SQLException ignore) {}
				}
		   }
		return fabProcessSplitForm;
	} 


	
	public ArrayList saveFab(DBTransaction dbTransaction,String [] fabProduct, String waferStore,String productId) throws ScalarException    
	{
		Connection conn = null;
		ResultSet rs    = null;
		PreparedStatement pstmt = null;
        ArrayList fabTrackList = new ArrayList();
		String trackId = "0";
		try
		{
			if (dbTransaction == null) 
			{
            	conn = DataSource.getConnection();
            }
            else 
            {
            	conn = dbTransaction.getConnection();
            }	
			//System.out.println("Query for Inseting fab "+SqlQueryMappings.get ("operations.process.product.insertFabtrack"));
		    
		    for( int i=0 ; i < fabProduct.length ; i++ )
		    {
				pstmt = conn.prepareStatement(SqlQueryMappings.get ("operations.process.product.insertFabtrack"));
			  	if( !fabProduct[i].equals("") )
				{	
    	            pstmt.setString(1,productId);
				    pstmt.setString(2,fabProduct[i]);
					pstmt.setString(3,"Incomplete");
					pstmt.executeUpdate();
					pstmt.close();
					
					pstmt = conn.prepareStatement(SqlQueryMappings.get("operations.process.upload.readIdentity"));
					rs = pstmt.executeQuery();

					while(rs.next())
					{
						trackId = rs.getString("docID");
					}
					
					rs.close();			
					if(waferStore.equals("0"))
					{
						pstmt.close();
						pstmt = conn.prepareStatement(SqlQueryMappings.get ("operations.process.product.fabProcessInsertWafer"));
						pstmt.setString(1,trackId);
                        // changed by hareesh on 6th October 2003
						//pstmt.setString(2,"WaferStore");
                        pstmt.setString(2,"Wafer Store");
						pstmt.setString(3,"Incomplete");
			
						pstmt.executeUpdate(); 
					}
					fabTrackList.add(trackId);	
				}	
				pstmt.close();
			} 
		}
		catch(Exception e)
		{
				e.printStackTrace();
				throw new ScalarException("tracking Fab failed ", e);
		}
		finally 
		{
			if (rs != null) {
				try { rs.close(); } catch (SQLException ignore) {}
			}
			if (pstmt != null) {
				try { pstmt.close(); } catch (SQLException ignore) {}
			}
			if (conn != null) {
				try { conn.close(); } catch (SQLException ignore) {}
			}
		}
		return fabTrackList;
	}


	// Ends Here

	// Delete the existing fab	
	public void deleteFab( DBTransaction dbTransaction , String productId ) throws ScalarException
	{
		Connection conn = null;
		ResultSet rs    = null;
		PreparedStatement pstmt=null;
        
		try
		{
			if (dbTransaction == null) 
			{
            	conn = DataSource.getConnection();
            }
            else 
            {
            	conn = dbTransaction.getConnection();
            }	
		
			pstmt = conn.prepareStatement(SqlQueryMappings.get ("operations.process.product.fabProcessDeleteSort"));
			pstmt.setString(1,productId);
			pstmt.executeUpdate();

			pstmt.close();
			pstmt = null;

			pstmt = conn.prepareStatement(SqlQueryMappings.get ("operations.process.product.fabProcessDelete"));
			pstmt.setString(1,productId);
			pstmt.executeUpdate();
		}
		catch (Exception sqle) 
		{
            sqle.printStackTrace();
            throw new ScalarException ("Could not Delete Fab from database", sqle);
        }
		finally
		{
			if (pstmt != null) {
                try { pstmt.close(); } catch (SQLException ignore){}
            }
            if (conn != null) {
                try { conn.close(); } catch (SQLException ignore){}
            }
		}	
    }

	//Ends Here

	public ArrayList save(String [] fabProduct, String waferStore,String productId) throws ScalarException  
   	{
		String checkProductId  = getProductId( productId );	
		ArrayList fabTrackList = new ArrayList();
		DBTransaction dbTransaction = null;
		boolean success = false;
		
		try
		{
			dbTransaction = DBTransaction.getInstance();
			dbTransaction.begin();		
				
           	if( checkProductId==null || checkProductId.equals("") )
           	{
				fabTrackList = saveFab( dbTransaction , fabProduct , waferStore , productId );
				success = true;
		    }
			else
			{
				 deleteFab( dbTransaction , productId );
				 fabTrackList = saveFab( dbTransaction , fabProduct , waferStore , productId );
				 success = true;
			}
			if (success == true) 
			{
            	dbTransaction.commit();
            }
            else 
            {
            	dbTransaction.rollback();
            }
		}
    	catch (Exception sqle) 
    	{
            sqle.printStackTrace();
            throw new ScalarException ("Could not save FabProcess SetUp from database", sqle);
		}
		return fabTrackList;
	}

}	